package cn.store.service.serviceImp;

import cn.store.dao.CategoryDao;
import cn.store.domain.Category;
import cn.store.service.CategoryService;
import cn.store.utils.BeanFactory;
import cn.store.utils.JDBCUtils;
import cn.store.utils.JedisUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import redis.clients.jedis.Jedis;

import java.sql.SQLException;
import java.util.List;

public class CategoryServiceImp implements CategoryService {

	CategoryDao CategoryDao=(CategoryDao) BeanFactory.createObject("CategoryDao");
	
	
	@Override
	public List<Category> getAllCats() throws Exception {
		return CategoryDao.getAllCats();
	}

	@Override
	public void addCategory(Category c) throws Exception {
		//本质是向MYSQL插入一条数据
		CategoryDao.addCategory(c);
		//更新redis缓存
		Jedis jedis = JedisUtils.getJedis();
		jedis.del("allCats");
		JedisUtils.closeJedis(jedis);
	}

	@Override
	public Category getCatById(String cid) throws Exception {
		return CategoryDao.getCatById(cid);
	}

	@Override
	public void updateCategory(Category c) throws SQLException {
		CategoryDao.updateCatById(c);
	}

	@Override
	public void deleteCategory(String cid) throws SQLException {
		CategoryDao.deleteCategory(cid);
	}
}
